package com.efast.dmom.mgmt.service.mes.order.impl;

import java.util.ArrayList;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.efast.cafe.framework.bean.ResultBean;
import com.efast.dmom.mgmt.bean.mes.order.RecycledVinBean;
import com.efast.dmom.mgmt.dao.mes.TwBasLotDao;
import com.efast.dmom.mgmt.entity.mes.TwBasLot;
import com.efast.dmom.mgmt.service.mes.VinRuleService;
import com.efast.dmom.mgmt.service.mes.order.VinPoolService;
import com.efast.dmom.mgmt.service.mes.order.VinRecycleService;
import com.efast.dmom.mgmt.utils.MesConstant;

@Service
public class VinPoolServiceImpl implements VinPoolService {

	@Autowired
	private TwBasLotDao lotDao;

	@Autowired
	private VinRuleService vinRuleService;

	@Autowired
	private VinRecycleService recycleService;

	//TODO
	private final int BATCH_COUNT = 1;
	private final String LOT_TYPE = "A";

	@Override
	public synchronized void requestNext() {

		TwBasLot basLot = lotDao.getNextLot(BATCH_COUNT, MesConstant.LOT_STATUS_CREATED, LOT_TYPE);

		if (basLot != null) {
			doLotBroadcast(basLot);
		}
	}
	

	@SuppressWarnings("unchecked")
	private void doLotBroadcast(TwBasLot basLot) {
		
		String vin = basLot.getVin();
				
		if(vin == null || "".equals(vin)){
					
			ResultBean resultBean = recycleService.fetchRecycledVin();
			List<RecycledVinBean> recyledBeanList = (List<RecycledVinBean>)resultBean.getObject();
			
			boolean recycled = false;
			if(recyledBeanList != null && recyledBeanList.size() > 0) {
				
				List<RecycledVinBean> tmpList = new ArrayList<RecycledVinBean>();
				
				for(RecycledVinBean vinBean : recyledBeanList){
					
					if(vinBean.getPartNoVersion() != null && vinBean.getPartNoVersion().equals(basLot.getPartNoVersion())){
						
						boolean isValid = vinRuleService.checkVin(vinBean.getPartNoVersion(), vinBean.getVin());
						if(isValid){
							basLot.setVin(vinBean.getVin());
							vinBean.setCurrentStatus(MesConstant.REUSED);
							tmpList.add(vinBean);
							
							recycleService.removeRecycledBean(vinBean);
							recycleService.updateRecycleBeanStatus(tmpList);
							recycled= true;
							
							break;
						}
					}
				}
			}
			if(! recycled)
			{
				List<String> vinList = vinRuleService.returnVinNum(basLot.getPartNoVersion(), 1);
				if(vinList != null && vinList.size() > 0)
				{
					basLot.setVin(vinList.get(0));
				}
			}
			lotDao.updateLot(basLot.getVin(), basLot.getLotId());
		}
	}

	@Override
	public synchronized int getCachedVinSize() {

		return lotDao.getGenerateVinCount(MesConstant.LOT_STATUS_CREATED, LOT_TYPE);
	}

	@Override
	public void vechicleScrap(List<RecycledVinBean> beanList) {
		
		if(beanList != null){
			recycleService.recycleVin(beanList);
		}
	}

	@Override
	public synchronized void holdLotInBatch(List<TwBasLot> beanList) {
		
		if(beanList != null){
			lotDao.hold(beanList);
		}
	}

	@Override
	public synchronized void releaseLotInBatch(List<TwBasLot> beanList) {

		if(beanList != null){
			lotDao.release(beanList);
		}
	}

	@Override
	public synchronized void recycleVin(List<RecycledVinBean> beanList) {
		
		recycleService.recycleVin(beanList);
	}


	@Override
	public synchronized void removeCachedRecycleBean(List<RecycledVinBean> beanList) {
		
		if(beanList != null)
		{
			for(RecycledVinBean vinBean : beanList)
			{
				recycleService.removeRecycledBean(vinBean);
			}
		}
	}

}
